package com.shangxian.giveithome.controller;


import com.shangxian.giveithome.annoaction.CheckLoginStatus;
import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.code.UserCode;
import com.shangxian.giveithome.entity.PutUser;
import com.shangxian.giveithome.entity.UserData;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.UserPerfect;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.UserService;
import com.shangxian.giveithome.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation(value = "根据session查询用户详细信息")
    @GetMapping(value = "/details/{id}")
    public Result queryDetails(@PathVariable Long id) {
        UserDetails userDetails = redisUtils.getHeader(request);
        if (!id.equals(userDetails.getId())) {
            ExceptionUtils.error(UserCode.USER_DETAILS_ERROR);
        }
        return Result.success(userDetails);
    }

    @ApiOperation(value = "完善用户基本信息")
    @PostMapping(value = "/perfect", name = "完善用户基本信息")
    public Result perfect(@Validated @RequestBody UserPerfect userPerfect) {
        userService.perfact(userPerfect, request);
        return Result.success();
    }

    @ApiOperation(value = "更新用户信息")
    @PutMapping(value = "/upUser", name = "更新用户信息")
    public Result PutUser(@RequestBody PutUser putUser) {
        return Result.success(userService.updateUser(putUser, request));
    }

    @ApiOperation(value = "查询已领养列表")
    @GetMapping(value = "/queryByAdoption", name = "查询已领养列表")
    public Result queryByAdoption(Pageable pageable) {
        UserDetails userDetails = redisUtils.getHeader(request);
        if (getUserData(userDetails.getId()).getAdoptionNumber() > 0) {
            return Result.success(userService.queryByAdoption(userDetails.getId(), pageable));
        }
        return Result.success(CommonCode.DATA_ERROR);
    }

    @ApiOperation(value = "查询已送养列表 0是待领养 1是已送养")
    @GetMapping(value = "/queryByPlace/{status}", name = "查询已送养列表 0是待领养 1是已送养")
    public Result queryByPlace(@PathVariable Integer status, Pageable pageable) {
        UserDetails userDetails = redisUtils.getHeader(request);
        if (getUserData(userDetails.getId()).getPlaceNumber() > 0) {
            if (status == 0) {
                return Result.success(userService.queryByPlace(userDetails.getId(), pageable));
            }
            if (status == 1) {
                return Result.success(userService.queryByPlaceHistory(userDetails.getId(), pageable));
            }
        }
        return Result.success(CommonCode.DATA_ERROR);
    }

    @ApiOperation(value = "查询寻主列表 0是未找到 1是已找到")
    @GetMapping(value = "/search_people/{status}", name = "查询寻主列表 0是未找到 1是已找到")
    public Result searchPeople(@PathVariable Integer status, Pageable pageable) {
        UserDetails userDetails = redisUtils.getHeader(request);
        if (getUserData(userDetails.getId()).getSearchPeople() > 0) {
            if (status == 0) {
                return Result.success(userService.searchPeople(userDetails.getId(), pageable));
            }
            if (status == 1) {
                return Result.success(userService.searchPeopleHistory(userDetails.getId(), pageable));
            }
        }
        return Result.success(CommonCode.DATA_ERROR);
    }

    @ApiOperation(value = "查询寻宠物列表 0是未找到 1是已找到")
    @GetMapping(value = "/search_pet/{status}", name = "查询寻宠物列表 0是未找到 1是已找到")
    public Result searchPet(@PathVariable Integer status, Pageable pageable) {
        UserDetails userDetails = redisUtils.getHeader(request);
        if (getUserData(userDetails.getId()).getSearchPet() > 0) {
            if (status == 0) {
                return Result.success(userService.searchPet(userDetails.getId(), pageable));
            }
            if (status == 1) {
                return Result.success(userService.searchPetHistory(userDetails.getId(), pageable));
            }
        }
        return Result.success(CommonCode.DATA_ERROR);
    }

    @ApiOperation(value = "查询我的活动 0正在进行中 1 历史")
    @GetMapping(value = "/queryMyActivity/{status}/{offSet}", name = "查询我的活动 0正在进行中 1 历史")
    public Result queryMyActivity(@PathVariable Integer status, @PathVariable Long offSet, Pageable pageable) {
        UserDetails userDetails = redisUtils.getHeader(request);
        if (status == 0) {
            return Result.success(userService.queryMyActivity(userDetails.getId(), offSet, pageable));
        }
        if (status == 1) {
            return Result.success(userService.queryMyActivityHistory(userDetails.getId(), offSet, pageable));
        }
        return Result.success(CommonCode.DATA_ERROR);
    }

//    @ApiOperation(value = "查询用户记录")
    public UserData getUserData(Long id) {
        return userService.queryUserData(id);
    }

    @ApiOperation(value = "根据用户id查询用户数据")
    @GetMapping(value = "/queryUserData", name = "根据用户id查询用户数据")
    public Result queryUserData() {
        UserDetails userDetails = redisUtils.getHeader(request);
        return Result.success(userService.queryUserData(userDetails.getId()));
    }

    //迁移---------------------------------
    @ApiOperation(value = "迁移用户数据")
    @GetMapping(value = "/moveuser", name = " 迁移用户数据")
    public Result moveuser() {

     return Result.success(userService.moveuser());
    }

    @ApiOperation(value = "迁移领养数据")
    @GetMapping(value = "/movepet", name = " 迁移领养数据")
    public Result movepet() {

        return Result.success(userService.movepet());
    }
    @ApiOperation(value = "迁移寻主数据")
    @GetMapping(value = "/movespeop", name = " 迁移领养数据")
    public Result movespeop() {

        return Result.success(userService.movespeop());
    }
    @ApiOperation(value = "迁移寻宠物数据")
    @GetMapping(value = "/movespet", name = " 迁移寻宠物数据")
    public Result movespet() {

        return Result.success(userService.movespet());
    }
    @ApiOperation(value ="通知中心")
    @GetMapping(value = "/noticeCenter",name = "用户通知中心" )
    @CheckLoginStatus
    public Result noticeCenter( HttpServletRequest request, @PageableDefault(page = 0,size = 10)Pageable pageable){
        return  Result.success(userService.noticeCenter(request,pageable));
    }

}
